import EventConfig from "../data/EventConfig";
import UIManager from "../manager/UIManager";
import EventName from "./EventName";
import OperateData from "./OperateData";
import UserData from "./UserData";


const { ccclass, property } = cc._decorator;

@ccclass
export default class ChooseEventWindow extends cc.Component {

    @property(cc.Sprite)
    icon: cc.Sprite = null;

    @property(cc.Label)
    lbName: cc.Sprite = null;

    @property(cc.Label)
    lbTitle: cc.Label = null;

    @property(cc.Node)
    content: cc.Node = null;

    @property(cc.Prefab)
    chooseItem: cc.Prefab = null;

    private _choosed = false;
    private _eventId = null;
    start() {
        cc.director.getScene().on(EventName.CHOOSE_BEHAVIOR, this.chooseBehaviro, this);
    }


    private chooseBehaviro(chooseIndex) {

        if (this._choosed) {
            return;
        }
        this._choosed = true;
        OperateData.chooseBehavior(this._eventId, chooseIndex);
        UIManager.closeWindow(ChooseEventWindow);
        UserData.getInstance().birthData.updateEvent(UserData.getInstance().birthData.age);
        cc.director.getScene().emit(EventName.UPDATE_GAME_CONTENT);
    }

    init() {
        this._choosed = false;
        this.updateUI();
    }

    private updateUI() {

        this._eventId = OperateData.getEventItemId();
        let eventItem = EventConfig.data[this._eventId];

        let behaviorList = eventItem.behaviorList;
        let content: string = eventItem.content;

        this.lbTitle.string = content;

        for (let i = 0; i < behaviorList.length; i++) {
            let item = cc.instantiate(this.chooseItem);
            item.setParent(this.content);
            item.getComponent("ChooseEventItem").init(i, behaviorList[i]);
        }

    }

    onClickClose() {
        UIManager.closeWindow(ChooseEventWindow);
    }

}
